﻿using DKPScoreWebApi.DTOs.Requests;
using DKPScoreWebApi.DTOs.Results;
using DKPScoreWebApi.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace DKPScoreWebApi.Controllers
{
    /// <summary>
    /// 认证控制器
    /// 处理用户登录和身份验证相关请求
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class AuthController : ControllerBase
    {
        private readonly IWechatService _wechatService;
        private readonly IGuildService _guildService;
        private readonly ILogger<AuthController> _logger;

        /// <summary>
        /// 构造函数，依赖注入相关服务
        /// </summary>
        public AuthController(IWechatService wechatService, IGuildService guildService, ILogger<AuthController> logger)
        {
            _wechatService = wechatService;
            _guildService = guildService;
            _logger = logger;
        }

        /// <summary>
        /// 微信登录接口
        /// POST /api/auth/login
        /// </summary>
        /// <param name="request">登录请求数据</param>
        /// <returns>登录结果，包含用户信息和管理的工会列表</returns>
        [HttpPost("login")]
        public async Task<ActionResult<ApiResult<object>>> Login([FromBody] LoginRequest request)
        {
            try
            {
                // 调用微信服务进行登录
                var user = await _wechatService.LoginAsync(request);

                // 获取用户管理的工会列表
                var guilds = await _guildService.GetUserGuildsAsync(user.Id);

                // 返回登录成功结果
                return Ok(new ApiResult<object>
                {
                    Success = true,
                    Data = new
                    {
                        user.Id,
                        user.OpenId,
                        user.Nickname,
                        user.AvatarUrl,
                        Guilds = guilds
                    }
                });
            }
            catch (Exception ex)
            {
                // 记录错误日志并返回错误信息
                _logger.LogError(ex, "用户登录失败");
                return BadRequest(new ApiResult { Success = false, Message = "登录失败" });
            }
        }

        /// <summary>
        /// 获取用户个人信息接口
        /// GET /api/auth/profile?userId=123
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>用户个人信息和管理的工会列表</returns>
        [HttpGet("profile")]
        public async Task<ActionResult<ApiResult<object>>> GetProfile(int userId)
        {
            try
            {
                // 获取用户信息
                var user = await _wechatService.GetUserByIdAsync(userId);
                if (user == null)
                    return NotFound(new ApiResult { Success = false, Message = "用户不存在" });

                // 获取用户管理的工会列表
                var guilds = await _guildService.GetUserGuildsAsync(userId);

                // 返回用户信息
                return Ok(new ApiResult<object>
                {
                    Success = true,
                    Data = new
                    {
                        user.Id,
                        user.OpenId,
                        user.Nickname,
                        user.AvatarUrl,
                        Guilds = guilds
                    }
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取用户信息失败");
                return BadRequest(new ApiResult { Success = false, Message = "获取用户信息失败" });
            }
        }
    }
}
